package com.fhj.movie.activity;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.fhj.movie.entity.FilmEntity;
import com.fhj.movie.entity.Stills;
import com.fhj.movie.interfaces.CallbackInterface;
import com.fhj.movie.interfaces.MyApplication;
import com.fhj.movie_fhj_01.R;
import com.fhj.utils.HttpConnectionUtils;
import com.fhj.utils.ImageDownload;
import com.fhj.utils.JsonUtils;

public class FHFilmDetailActivity extends Activity implements OnClickListener {

	private FilmEntity filmEntity;
	private ImageView film_detail_big_image;
	private LinearLayout film_detail_bottom_btn;
	private TextView film_detail_bottom_buy_now;
	private LinearLayout film_detail_bottom_buy_now_btn;
	private TextView film_detail_bottom_wantsee;
	private ListView film_detail_comment_listview;
	private NetworkImageView film_detail_image_juzhao;
	private ImageView film_detail_is_3d_hint;
	private ImageView film_detail_is_video_play;
	private RelativeLayout film_detail_layout_topcomment;
	private TextView film_detail_film_area;
	private TextView film_detail_film_director;
	private TextView film_detail_film_lasting;
	private TextView film_detail_film_play_date;
	private TextView film_detail_film_player;
	private TextView film_detail_film_rate;
	private TextView film_detail_film_summary;
	private TextView film_detail_film_type;
	private TextView film_detail_no_comments;
	private Button film_detail_say_word;
	private TextView film_detail_story_desc;
	private TextView film_detail_top_name;
	private ImageView film_detail_top_share;
	private TextView film_detail_tv_juzhao_count;
	private TextView film_detail_tv_longcomment;
	private TextView film_detail_tv_release_time;
	private TextView film_detail_tv_want_see_count;
	private int position;
	private int type;
	
	private ImageDownload mImageDownload;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.film_detail_layout);
		mImageDownload = ImageDownload.getInstance(this);
		film_detail_top_share = ((ImageView) findViewById(R.id.film_detail_share));
		film_detail_top_share.setOnClickListener(this);
		film_detail_big_image = ((ImageView) findViewById(R.id.film_detail_image));
		film_detail_big_image.setOnClickListener(this);
		film_detail_image_juzhao = ((NetworkImageView) findViewById(R.id.film_detail_juzhao));
		film_detail_say_word = ((Button) findViewById(R.id.film_detail_say_word));
		film_detail_say_word.setOnClickListener(this);
		film_detail_top_name = ((TextView) findViewById(R.id.film_detail_filmname));
		film_detail_is_3d_hint = ((ImageView) findViewById(R.id.film_detail_is_3dimax));
		film_detail_is_video_play = ((ImageView) findViewById(R.id.film_detail_preplay));
		film_detail_is_video_play.setOnClickListener(this);
		film_detail_film_summary = ((TextView) findViewById(R.id.film_detail_short_intro));
		film_detail_tv_juzhao_count = ((TextView) findViewById(R.id.film_detail_juzhao_count));
		film_detail_film_rate = ((TextView) findViewById(R.id.film_detail_rating_sorce));
		// this.film_detail_tv_want_see_count = ((TextView)findViewById());
		film_detail_film_play_date = ((TextView) findViewById(R.id.film_detail_shy_time));
		film_detail_film_director = ((TextView) findViewById(R.id.film_detail_daoyan));
		film_detail_film_player = ((TextView) findViewById(R.id.film_detail_zhuyan));
		film_detail_tv_release_time = ((TextView) findViewById(R.id.film_detail_shy_time));
		film_detail_film_area = ((TextView) findViewById(R.id.fiml_detail_area));
		film_detail_film_type = ((TextView) findViewById(R.id.fiml_detail_catagory));
		film_detail_film_lasting = ((TextView) findViewById(R.id.fiml_detail_duration));
		film_detail_story_desc = ((TextView) findViewById(R.id.film_detail_juqing));
		
		film_detail_bottom_wantsee = ((TextView) findViewById(R.id.film_detail_want_see));
		film_detail_bottom_buy_now = ((TextView) findViewById(R.id.film_detail_buy_now));

		Intent localIntent = getIntent();
		MyApplication application = (MyApplication) getApplication();
		int cityId = application.getCityCode();
		String mode = localIntent.getStringExtra("mode");
		type = localIntent.getIntExtra("type", -1);
		movieId = localIntent.getIntExtra("movieId", -1);
		Log.i("-------","-------->"+movieId);
		

		HttpConnectionUtils.getFilmDetailData(this, movieId, cityId,
				new CallbackInterface() {

					@Override
					public void JsonCallback(JSONObject object) {
						// TODO Auto-generated method stub
						Log.i("filmJson", object.toString());
						filmEntity = JsonUtils.parseFilmDetail(object);
						if (filmEntity != null) {
							Message message = handler.obtainMessage(1);
							handler.sendMessage(message);
						}

					}
				});

	}

	private void updateView(FilmEntity paramfilmInfo) {
		this.film_detail_top_name.setText(paramfilmInfo.getName());
		 mImageDownload.downLoadImage(this.film_detail_big_image,paramfilmInfo.getLogo556640());
		List localList = paramfilmInfo.getStillsList();
		String str;
		if ((localList != null) && (localList.size() > 0)) {
			mImageDownload.downLoadImage(
					this.film_detail_image_juzhao,
					((Stills) localList.get(0)).getLogo1());
			this.film_detail_tv_juzhao_count.setText(localList.size() + "张");
			// this.film_detail_tv_juzhao_count.setVisibility(View.VISIBLE);
//			str = paramfilmInfo.getDimensional();
//			if (!"3D".equals(str))
//				this.film_detail_is_3d_hint.setVisibility(View.GONE);
//			// this.film_detail_is_3d_hint.setImageDrawable(getResources().getDrawable(2130837853));
//			// this.film_detail_is_3d_hint.setVisibility(0);
//			if (TextUtils.isEmpty(paramfilmInfo.getMobilePreview()))
//				this.film_detail_is_video_play.setVisibility(View.GONE);
		}

		this.film_detail_film_summary.setText(paramfilmInfo.getHighLight());
		this.film_detail_film_rate.setText(paramfilmInfo.getGrade() + "分");
		this.film_detail_film_play_date.setText(paramfilmInfo.getReleaseDate());
		this.film_detail_film_director.setText(paramfilmInfo.getDirector());
		this.film_detail_film_player.setText(paramfilmInfo.getActors());
		this.film_detail_tv_release_time.setText(paramfilmInfo.getScreenings());
		this.film_detail_film_area.setText(paramfilmInfo.getAreaString());
		this.film_detail_film_type.setText(paramfilmInfo.getCategory());
		this.film_detail_film_lasting.setText(paramfilmInfo.getDuration());
		this.film_detail_story_desc.setText(paramfilmInfo.getDescription());
		// this.film_detail_bottom_btn.setVisibility(0);
		if (strToBoolean(Integer.toString(paramfilmInfo.getOnShowStatus())))
		{
			this.film_detail_bottom_buy_now.setVisibility(View.VISIBLE);
			this.film_detail_bottom_wantsee.setVisibility(View.GONE);
		}

		// if ("3D,IMAX".equals(str))
		// {
		// this.film_detail_is_3d_hint.setImageDrawable(getResources().getDrawable(2130837854));
		// this.film_detail_is_3d_hint.setVisibility(0);
		// }

		// if ("2D,IMAX".equals(str))
		// {
		// this.film_detail_is_3d_hint.setImageDrawable(getResources().getDrawable(2130837852));
		// this.film_detail_is_3d_hint.setVisibility(0);
		// break label148;
		// }

	}

	private boolean strToBoolean(String paramString) {
		return Integer.parseInt(paramString) == 1;
	}

	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.film_detail_preplay:
			Intent videoIntent = new Intent(this,FHVideoPlayActivity.class);
			videoIntent.putExtra("videoUrl", filmEntity.getMobilePreview());
		      startActivity(videoIntent);
		      return;

		}
		Toast.makeText(this, "不能点击", 0).show();
	}

	private int movieId;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg){
			int what = msg.what;
			if (what == 1) {
				updateView(filmEntity);
			}
		};
	};
}
